class ListNode:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next


# 输入列表，通过递归函数构建链表
def buildList(nums):
    def build(i):
        if i == len(nums): return None
        head = ListNode(nums[i])
        head.next = build(i+1)
        return head

    return build(0)


# 遍历链表
def traverse(p):
    while p is not None:
        print(p.val)
        p = p.next


# 测试
p = buildList([1, 2, 3, 4, 5])
traverse(p)

